package com.winshare.saleAssistant.util;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;

public class SecurityManager {

    public static final int HASH_INTERATIONS = 1024;
    public static final int SALT_SIZE = 8;

    public SecurityManager() {
    }

    public static String entryptSha1(String plainText, boolean toLowerCase) {
        byte[] salt = Digests.generateSalt(8);
        byte[] hashText = Digests.sha1(plainText.getBytes(), salt, 1024);
        return Encodes.encodeHex(salt, toLowerCase) + Encodes.encodeHex(hashText, toLowerCase);
    }

    public static String entryptSha256(String plainText, boolean toLowerCase) {
        byte[] hashText = Digests.sha256(plainText.getBytes());
        return Encodes.encodeHex(hashText, toLowerCase);
    }

    public static String entryptMd5(String plainText, boolean toLowerCase) {
        byte[] salt = Digests.generateSalt(8);
        byte[] hashText = Digests.md5(plainText.getBytes(), salt, 1024);
        return Encodes.encodeHex(salt, toLowerCase) + Encodes.encodeHex(hashText, toLowerCase);
    }

    public static boolean decryptSha1(String plainText, String entryText, boolean toLowerCase) {
        byte[] salt = new byte[0];

        try {
            salt = Encodes.decodeHex(entryText.substring(0, 16));
        } catch (DecoderException var5) {
            var5.printStackTrace();
        }

        byte[] hashText = Digests.sha1(plainText.getBytes(), salt, 1024);
        return entryText.equals(Encodes.encodeHex(salt, toLowerCase) + Encodes.encodeHex(hashText, toLowerCase));
    }

    public static boolean decryptMd5(String plainText, String entryText, boolean toLowerCase) {
        byte[] salt = new byte[0];

        try {
            salt = Encodes.decodeHex(entryText.substring(0, 16));
        } catch (DecoderException var5) {
            var5.printStackTrace();
        }

        byte[] hashText = Digests.md5(plainText.getBytes(), salt, 1024);
        return entryText.equals(Encodes.encodeHex(salt, toLowerCase) + Encodes.encodeHex(hashText, toLowerCase));
    }

    public static String entryptBase64(String plainText) throws EncoderException {
        return Encodes.encodeBase64(plainText);
    }

    public static String entryptBase64(byte[] bytes) {
        return Encodes.encodeBase64(bytes);
    }

    public static String decryptBase64(String text) throws DecoderException {
        return Encodes.decodeBase64String(text);
    }

    public static byte[] decryptBase64ToByte(String text) throws DecoderException {
        return Encodes.decodeBase64(text);
    }

    public static String entryptAes(String plainText) {
        return Cryptos.aesEncrypt(plainText);
    }

    public static String decryptAes(String entryText) {
        return Cryptos.aesDecrypt(entryText);
    }
}
